package controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import model.Shop;
import model.ShopDao;
import model.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.DatagramPacket;
import java.util.List;

@WebServlet("/shops")
public class ShopServlet extends HttpServlet {
    //使用这个数据类型来将数据转成json格式
    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        resp.setCharacterEncoding("utf8");
        resp.setContentType("text/html;charset=utf-8");

        String category = req.getParameter("category");

        //在数据库中查询商品信息，并通过json格式返回
        ShopDao shopDao = new ShopDao();
        List<Shop> shops = shopDao.selectAllShops(category);
        String respJson = objectMapper.writeValueAsString(shops);
        System.out.println(respJson);
        resp.getWriter().write(respJson);





    }
}
